package cn.yanhu.kuwanapp.utils.ext

import android.os.Build
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.TextView
import android.widget.Toast
import cn.woochen.comlib.util.ApplicationUtil
import cn.woochen.comlib.util.SystemAppUtil
import cn.woochen.comlib.util.ext.dp
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.http.UrlConstainer
import com.hjq.toast.ToastUtils
import java.util.*

private var lastToastTime = 0L
private var lastToastContent = ""

/**
 * 自定义toast
 */
fun Any.showToast(msg: String?,isLong:Boolean = false) {
    if (msg.isNullOrBlank()) return
    if (lastToastContent == msg && System.currentTimeMillis() - lastToastTime < 2000){
        return
    }else{
        lastToastTime = System.currentTimeMillis()
        lastToastContent = msg
    }
    val check = Build.VERSION.SDK_INT > Build.VERSION_CODES.P || SystemAppUtil.notifycationEnable(
        ApplicationUtil.context) || "xiaomi" == Build.MANUFACTURER.toLowerCase(Locale.ROOT) //当大于28，或者通知权限已经打开，或者是小米系统直接使用系统的toast
    if (check){
        try {
            val inflater = LayoutInflater.from(ApplicationUtil.context)
            val view = inflater.inflate(R.layout.toast_view, null) as TextView
            view.text = msg
            val toast = Toast(ApplicationUtil.context)
            toast.duration = if (isLong) Toast.LENGTH_LONG else Toast.LENGTH_SHORT
            toast.view = view
            toast.setGravity(Gravity.BOTTOM, 0, (50f.dp).toInt())
            toast.show()
        } catch (e: Exception) {
        }
    }else{
        ToastUtils.show(msg)
    }
}


fun Any.showToast(stringId: Int) {
    try {
        showToast(ApplicationUtil.context.getString(stringId))
    } catch (e: Exception) {

    }
}

/**
 * 常规日志打印
 * 过滤关键字 logcom
 */
fun logcom(msg: String?){
    if (UrlConstainer.SHOW_LOG && msg != null) Log.v("LogCom",msg)
}

/**
 * 流程日志打印
 * 过滤关键字 logprocess
 */
fun logprocess(msg: String?){
    if (UrlConstainer.SHOW_LOG && msg != null) Log.i("LogProcess",msg)

}